local hx__fangquan_chen = fk.CreateSkill {
  name = "hx__fangquan_chen",
  tags = {  },
  dynamic_desc = function (self, player, lang)
    local viewas
    for _, skill in ipairs(self.effects) do
      if skill:isInstanceOf(ViewAsSkill) then
        viewas = skill
        break
    end
    end
    if player:usedEffectTimes(viewas.name, Player.HistoryGame) > 0 then
      return "hx__fangquan_chen_after"
    else
      return "hx__fangquan_chen"
    end
  end,
}


hx__fangquan_chen:addEffect("viewas", {
  prompt = "#hx__fangquan_chen",
  mute_card = false,
  interaction = function(self, player)
    local all_names = {"duel"}
    local can_use = player:getViewAsCardNames(hx__fangquan_chen.name, all_names, player:getCardIds("hej"))
    if #can_use == 0 then return end
    return UI.CardNameBox{ choices = can_use , all_choices = all_names }
  end,
  filter_pattern = function (self, player, card_name)
    local cards = player:getCardIds("hej")
    return {
      max_num = #cards,
      min_num = #cards,
      pattern = ".",
      subcards = cards,
    }
  end,
  card_filter = Util.FalseFunc,
  before_use = function (self, player, use)
    local choices = {"hx__fangquan_chen_duel_1", "hx__fangquan_chen_duel_nores"}
    local choice = player.room:askToChoice(player, {
      choices = choices,
      skill_name = hx__fangquan_chen.name,
      cancelable = false,
    })
    if choice == "hx__fangquan_chen_duel_nores" then
      use.disresponsiveList = player.room.players
    elseif choice == "hx__fangquan_chen_duel_1" then
      use.additionalDamage = (use.additionalDamage or 0) + 1
    end
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(player:getCardIds("hej"))
    card.skillName = hx__fangquan_chen.name
    return card
  end,
  after_use = function (self, player, use)
    player.room:setPlayerMark(player, "@@hx__fangquan_chen_skip2", 1)
  end,
  enabled_at_play = function(self, player)
    return player:usedEffectTimes(self.name, Player.HistoryGame) == 0 and not player:isAllNude()
  end,
})

hx__fangquan_chen:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__fangquan_chen.name) and data.phase == Player.Start then
      return table.find(player.room.alive_players, function(p) return player:getMark("hx__fangquan_chen"..player.id) ~= p.id end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(player.room.alive_players, function(p) return player:getMark("hx__fangquan_chen"..player.id) ~= p.id end)
    if #targets == 0 then return false end
    local choice = room:askToChoice(player, {
      choices = {"Cancel", "hx__fangquan_chen_draw", "hx__fangquan_chen_skip"},
      skill_name = hx__fangquan_chen.name,
      prompt = "放权：你可以选择一项，然后选择一名角色执行此项（不能选择上个准备阶段选择的角色）",
      cancelable = true,
    })
    if choice == "Cancel" then return false end
    local to = room:askToChoosePlayers(player, {
      targets = targets,
      max_num = 1,
      min_num = 1,
      skill_name = hx__fangquan_chen.name,
      prompt = "放权：请选择一名角色执行该效果",
      cancelable = false,
    })
    if #to > 0 then
      event:setCostData(self, { to = to[1], choice = choice })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to
    local choice = event:getCostData(self).choice
    room:setPlayerMark(player, "hx__fangquan_chen"..player.id, to.id)
    if choice == "hx__fangquan_chen_draw" then
      room:addPlayerMark(to, "@@hx__fangquan_chen_draw-round", 1)
    elseif choice == "hx__fangquan_chen_skip" then
      room:addPlayerMark(to, "@@hx__fangquan_chen_skip-round", 1)
    end
  end,
  late_refresh = true, --trigger之后可触发
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(hx__fangquan_chen.name) and data.phase == Player.Start
  end,
  on_refresh = function(self, event, target, player, data)
    if player:usedEffectTimes(self.name, Player.HistoryPhase) == 0 then
      player.room:setPlayerMark(player, "hx__fangquan_chen"..player.id, 0)
    end
  end,
})


hx__fangquan_chen:addEffect(fk.EventPhaseChanging, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player then
      if data.phase == Player.Discard then
        return player:getMark("@@hx__fangquan_chen_skip-round") ~= 0
      elseif data.phase == Player.Start then
        return player:getMark("@@hx__fangquan_chen_skip2") ~= 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if data.phase == Player.Start then
      player.room:setPlayerMark(player, "@@hx__fangquan_chen_skip2", 0)
    end
    data.skipped = true
  end,
})

hx__fangquan_chen:addEffect(fk.DrawNCards, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@hx__fangquan_chen_draw-round") ~= 0
  end,
  on_use = function(self, event, target, player, data)
    data.n = data.n + 2
  end,
})


return hx__fangquan_chen